<?php
include_once("player.php");
if(class_exists('SQL_Helper') != true)
{
   include_once("sql_helper.php");
}
class PlayerRepository
{
function createPlayer($fname, $sname, $email){
	SQL_Helper::executeNonQuery("INSERT INTO `players` (`playerID`, `fname`, `sname`, `email`, `status`) VALUES(null,?,?,?,'Active')",$fname, $sname, $email);
}

function readPlayer($playerId){
	return SQL_Helper::executeQuerySingle("Select * from `players` where `playerID` = ?", $playerId);
}

function updatePlayer($playerId, $fname, $sname, $email, $status){
	SQL_Helper::executeNonQuery("Update `players` set `fname`=?, `sname`=?, `email`=?, `status`=? where `playerID` = ?",$fname, $sname, $email, $status, $playerId);
}
function deleteMatch($playerId){
	SQL_Helper::executeNonQuery("delete from `players` where `playerID` = ?", $playerId);
	SQL_Helper::executeNonQuery($deleteQuery);
}

function map($row){
	$class= "Player";
	return SQL_Helper::sql_map($row,$class);
}
function maps($rows){
		$class= "Player";
		return SQL_Helper::sql_maps($rows,$class);
	}
	
function getActivePlayers(){
	return SQL_Helper::executeQuery("Select * from `players` where `status` = 'Active'");
}

function getAvailablePlayers($roundID){
	return SQL_Helper::executeQuery("SELECT * FROM `players` WHERE `status`='Active' and `playerID` not in (Select `player1ID` from `match` where `round_scheduleID` in (select `round_scheduleID` from `round_schedule` where roundID=?) ) and `playerID` not in (Select `player2ID` from `match` where `round_scheduleID` in (select `round_scheduleID` from `round_schedule` where roundID=?) )",$roundID,$roundID);
}
function getPlayersByRounds($firstRoundID, $lastRoundID,$divisionID){
	$row= SQL_Helper::executeQuery("SELECT DISTINCT `player1ID` as `playerID` FROM  `match` WHERE  `round_scheduleID` IN (SELECT  `round_scheduleID` FROM  `round_schedule` WHERE  `roundID` >=? AND  `roundID` <=? and `team1` in (select `teamID` from `teams` where `divisionID`=?)) union SELECT DISTINCT `player2ID` as `playerID` FROM  `match` WHERE  `round_scheduleID` IN (SELECT  `round_scheduleID` FROM  `round_schedule` WHERE  `roundID` >=? AND  `roundID` <=?  and `team2` in (select `teamID` from `teams` where `divisionID`=?))",$firstRoundID, $lastRoundID,$divisionID,$firstRoundID, $lastRoundID,$divisionID);
	foreach ($row  as $r)
	{
		$playerId[]=$r['playerID'];
	}
	
	return $playerId;
}

function getAllPlayers(){
	return SQL_Helper::executeQuery("Select * from `players` order by `sname` ");
}

function getAvailablePlayersForTeam(){
	return SQL_Helper::executeQuery("SELECT * FROM `players` WHERE `playerID` not in (select `playerID` FROM `teammembers` inner join `teams` on `teammembers`.`teamID`= `teams`.`teamID` and `team_status`='Active')");
}

}
?>